import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mes_app/common/index.dart';
import 'package:get/get.dart';

import 'index.dart';

class DesktopPage extends GetView<DesktopController> {
  const DesktopPage({super.key});

  Widget _buildGridItem(BuildContext context, IcosItem item) {
    return InkWell(
      onTap: () {
        print("item$item");
      },
      child: <Widget>[
        Container(
          width: 55.w,
          height: 55.w,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: item.colors,
              stops: const [0.0, 1.0],
              transform: const GradientRotation(178 * 3.1415926535 / 180),
            ),
          ),
          child: IconWidget.svg(
            item.iconData,
            size: 30.sp,
            color: context.colors.scheme.onPrimary,
            fit: BoxFit.contain,
          ).center(),
        ).paddingBottom(AppSpace.column),
        TextWidget.label(
          item.label,
          overflow: TextOverflow.ellipsis,
        )
      ].toColumn(
        mainAxisAlignment: MainAxisAlignment.center,
      ),
    );
  }

  Widget _buildCard(BuildContext context, String title, List<IcosItem> items) {
    return SliverToBoxAdapter(
      child: <Widget>[
        TextWidget.h4(
          title,
          textAlign: TextAlign.start,
        ).paddingLeft(20.w).paddingBottom(10.w),
        GridView.builder(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
          ),
          itemCount: items.length,
          itemBuilder: (buildContext, index) =>
              _buildGridItem(context, items[index]),
        ),
      ]
          .toColumn(crossAxisAlignment: CrossAxisAlignment.start)
          .paddingVertical(AppSpace.card.w)
          .card(elevation: AppElevation.card),
    )
        .sliverPaddingHorizontal(AppSpace.page)
        .sliverPaddingVertical(AppSpace.page);
  }

  // 主视图
  Widget _buildView(BuildContext context) {
    return CustomScrollView(
      slivers: [
        _buildCard(context, LocaleKeys.desktopBusinessFunction.tr,
            controller.businessFunctionItems),
        _buildCard(context, LocaleKeys.desktopWarehouseManagement.tr,
            controller.warehouseManagementItems),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DesktopController>(
      init: DesktopController(),
      id: "desktop",
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            title: Text(LocaleKeys.desktopIndexTitle.tr),
          ),
          body: SafeArea(
            child: _buildView(context),
          ),
        );
      },
    );
  }
}
